﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineGameClient.ViewModel;

namespace OnlineGameClient.View
{
    public partial class LoginWindow : ChildWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
            this.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(LoginWindow_Closing);
        }

        void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!this.DialogResult.HasValue)
                e.Cancel = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (logintb.Text.Length > 10)
                MessageBox.Show("login name too long");

            var vm = this.DataContext as GameRoomViewModel;

            vm.GameServiceClientProxy.GameService.BeginPlayerLogin(
                logintb.Text,
                new AsyncCallback((ar) =>
                {
                    Dispatcher.BeginInvoke(delegate
                    {
                        vm.LoginPlayer =
                            vm.GameServiceClientProxy.GameService.EndPlayerLogin(ar);
                        vm.LoadGameInfoListCommand.Execute(null);
                        vm.GetGameRoomsCommand.Execute(null);
                        this.DialogResult = true;
                    });
                }),
            null);
        }
    }
}

